//
//  TYZControl.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/7/29.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "TYZControl.h"

@implementation TYZControl
{
    UIImage *_image;
    CGPoint _point;
    NSTimer *_timer;
    BOOL _longPressDetected; ///< 长按检测
}

- (void)dealloc
{
    [self endTimer];
}

- (void)setImage:(UIImage *)image
{
    _image = image;
    self.layer.contents = (id)image.CGImage;
}

- (UIImage *)image
{
    id content = self.layer.contents;
    if (content != (id)_image.CGImage)
    {
        CGImageRef ref = (__bridge CGImageRef)(content);
        if (ref && CFGetTypeID(ref) == CGImageGetTypeID())
        {
            _image = [UIImage imageWithCGImage:ref scale:self.layer.contentsScale orientation:UIImageOrientationUp];
        }
        else
        {
            _image = nil;
        }
    }
    
    return _image;
}

- (void)startTimer
{
    [_timer invalidate];
    _timer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(timerFire) userInfo:nil repeats:NO];
}

- (void)endTimer
{
    [_timer invalidate];
    _timer = nil;
}

- (void)timerFire
{
    [self touchesCancelled:[NSSet set] withEvent:nil];
    _longPressDetected = YES;
    if (_longPressBlock)
    {
        _longPressBlock(self, _point);
    }
    [self endTimer];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    _longPressDetected = NO;
    if (_touchBlock)
    {// YYGestureRecognizerStateBegan
        _touchBlock(self, YYGestureRecognizerStateBegan, touches, event);
    }
    
    if (_longPressBlock)
    {
        UITouch *touch = touches.anyObject;
        _point = [touch locationInView:self];
        [self startTimer];
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_longPressDetected)
    {
        return;
    }
    if (_touchBlock)
    {
        _touchBlock(self, YYGestureRecognizerStateMoved, touches, event);
    }
    [self endTimer];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_longPressDetected)
    {
        return;
    }
    if (_touchBlock)
    {
        _touchBlock(self, YYGestureRecognizerStateEnded, touches, event);
    }
    [self endTimer];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (_longPressDetected)
    {
        return;
    }
    if (_touchBlock)
    {
        _touchBlock(self, YYGestureRecognizerStateCancelled, touches, event);
    }
    [self endTimer];
}

@end


































